import React, { memo } from "react";
import { LockOutlined, UserOutlined } from "@ant-design/icons";
import { Button, Form, Input, message, Modal } from "antd";
import { FormInstance } from "antd/es/form/Form";
import { passwordrules, usernamerules } from "@/utils/formrules";
import { useDispatch } from "react-redux";
import { login } from "@/apis/user";
import { getcollectidarraction, loginpatch } from "@/store/action/home";
import "./login.less";
interface propsv {
  loginvisiable: boolean;
  toregistermodel: Function;
  loginform: FormInstance<any>;
  setLogininvisiable: Function;
}
function Login(props: propsv) {
  const { loginvisiable, toregistermodel, loginform, setLogininvisiable } =
    props;
  const dispatch = useDispatch();
  //登录
  const loginonFinish = async (value: any) => {
    // console.log(value);
    const result: any = await login(value);
    localStorage.setItem("jiang_blog_token", result.token);
    if (result.status === 401) {
      return message.error("用户名或密码错误");
    }
    dispatch(loginpatch(result.user));
    dispatch<any>(getcollectidarraction(result.user.id));
    message.success("登录成功");
    setLogininvisiable(false);
  };
  //gitee三方登录
  const giteelogin = async () => {
    window.location.href =
      "https://gitee.com/oauth/authorize?client_id=8bfb80fe67e8b3460d1233a3407f73506dead1f0d7634b768ad2b2e6728bab38&redirect_uri=http://localhost:8000/user/giteelogin&response_type=code";
  };
  return (
    <Modal
      visible={loginvisiable}
      footer={null}
      onCancel={(e) => setLogininvisiable(false)}
      className="login-modal-content"
    >
      <div className="top">
        <div className="left">
          <span>欢迎你,</span>
          <br />
          请填写以下信息进行登录
        </div>
        <span className="right" onClick={(e) => toregistermodel()}>
          注册
        </span>
      </div>
      <div className="form">
        <Form
          form={loginform}
          name="normal_login"
          className="login-form"
          initialValues={{ remember: true }}
          onFinish={loginonFinish}
          labelCol={{
            span: 4,
          }}
        >
          <Form.Item name="username" label="用户名" rules={usernamerules}>
            <Input
              prefix={<UserOutlined className="site-form-item-icon" />}
              placeholder="Username"
            />
          </Form.Item>
          <Form.Item name="password" label="密码" rules={passwordrules}>
            <Input
              prefix={<LockOutlined className="site-form-item-icon" />}
              type="password"
              placeholder="Password"
            />
          </Form.Item>
          <Form.Item>
            <Button
              type="primary"
              htmlType="submit"
              size={"large"}
              className="login-form-button"
            >
              登录
            </Button>
          </Form.Item>
        </Form>
      </div>
      <div className="fiveloginfont">
        <span className="iconfont icon-github-fill"></span>
        <span className="iconfont icon-QQ-circle-fill"></span>
        <span className="iconfont icon-icon_baidulogo"></span>
        <span className="iconfont icon-shejiaotubiao-42"></span>
        <span className="iconfont icon-gitee" onClick={giteelogin}></span>
      </div>
    </Modal>
  );
}

export default memo(Login);
